import 'package:flutter/material.dart';
import 'package:kasax/commons/commons.dart';
import 'package:rxdart/rxdart.dart';

/// 重复生成列表
/// https://blog.csdn.net/jielundewode/article/details/94381616
class SampleUiWithRxDartDemoHome extends StatefulWidget {
  const SampleUiWithRxDartDemoHome({Key? key}) : super(key: key);

  @override
  _SampleUiWithRxDartDemoHomeState createState() =>
      _SampleUiWithRxDartDemoHomeState();
}

class _SampleUiWithRxDartDemoHomeState
    extends State<SampleUiWithRxDartDemoHome> {
  var subject = ReplaySubject<List<String>>();

  @override
  void initState() {
    super.initState();
    subject.delay(Duration(seconds: 10));
    subject.add(['a', 'b', 'c', 'd']);
  }

  @override
  void dispose() {
    super.dispose();
    subject.close();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: (Text("动态列表生成")),
      ),
      body: Center(
        child: StreamBuilder(
          stream: subject.stream,
          builder:
              (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
            print("snapshot-state: ${snapshot.connectionState}");
            if(!snapshot.hasData){
              return buildLoadingWidget();
            }
            return ListView.separated(itemBuilder: (context,index){
              return ListTile(title: Text(snapshot.data![index],style: TextStyle(fontSize: 20),textAlign: TextAlign.center,),);
            }, separatorBuilder: (context,index){return Divider();}, itemCount: snapshot.data!.length);
          },
        ),
      ),
    );
  }

  Column buildColumn(AsyncSnapshot<List<String>> snapshot) {
    return Column(
            children: snapshot.data!
                .map((e) => Text(
                      e,
                      style: TextStyle(fontSize: 30),
                    ))
                .toList(),
          );
  }
}
